<?php 
namespace app\supermanager\controller;

use think\Validate;
use think\Controller;
use app\common\traits\controller\TraitController;
/**
 * 总公司管理员设置类
 */
class ManagerSet extends Controller
{
	use TraitController;
	protected $beforeActionList = [
        'limitRights'=> ['except' => 'getresetpwd,postsaveresetpwd,returnajax,getcreateinfo,postsaveinfo'],
    ];
	/**
	 * 权限限制
	 */
	protected function limitRights(){
	    if(model('Managers')->getLoginUserInfo('type')!==0){
          	echo view('index/prompt')->Send();
            exit;
        }
	}
	/**
	 * 初始化操作
	 */
	protected function init()
    {
        if (!model('Managers')->checkLogin()) {
            $this->redirect('@manager/login');
            exit;
        }
    }

	public function index(){
		$managerInfo = model('Managers')->getCaravansLists(input('get.'));
		$this->assign("managerInfo",$managerInfo);
	    return view('managerset/index');
	}
	/**
	 * 添加管理员表单
	 * @param type 管理员类型 0->总管理员 1->下属管理员
	 * 管理员初始密码 ==> 管理员账号
	 */
	 public function save(){
		foreach ($info = $this->request->post() as &$v) {
			$v = str_replace(' ', '', $v);			
		}
		$result = $this->validate($info,'ManagerSet');
		if(true !== $result){
		 	return $this->error($result);   
		}
		if(model('Managers')->get(['account' => $info['account']])){
	 		return $this -> error('管理员账号已存在');
	 	};
		$manager_info_array['password'] = md5($info['account']);
		$manager_info_array['type'] = 1;
		if(model('Managers')->addManagerInfo(array_merge($info,$manager_info_array))){
			$this->redirect('superManager/ManagerSet/index');
		}
		$this -> error('添加失败');
	 }
	 
	 /**
	  * 添加管理员页面
	  */
	public function create(){
	    return view('managerset/create');
	}
	
	/**
	 * 删除管理员表单
	 */
	public function del(){
		if(empty(input('post.ids/a'))){
			return json($this->returnAjax(0,'请选择删除的内容'));
		}
		if(model('Managers')->delManager(input('post.ids/a'))){
			return json($this->returnAjax(1,'删除成功'));
		}
	}
	
	/**
	 * 管理员个人资料表单
	 */
	public function postSaveInfo(){
		$id_card = '/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/';
		if(false === Validate::regex(input('post.identity_num'),$id_card))
		{
			return $this -> error('身份证号格式错误');	
		}
		if(false === Validate::regex(input('post.mobile'),'^(13[0-9]|14[5|7]|15[0-9]|18[0-9]|17[0-9])\d{8}$'))
		{
			return $this -> error('手机号格式错误');	
		}
		$manager_info_array['mobile'] = str_replace(' ', '', input('post.mobile'));
		$manager_info_array['address'] = input('post.address');
		$manager_info_array['identity_num'] = input('post.identity_num');
		if(model('Managers')->updateInfo($manager_info_array,model('Managers')->getLoginUserInfo('id'))){
			$this->success('修改个人资料成功');
		}
		$this -> error('修改失败');
	} 
	
	/**
	 * 管理员个人资料页面
	 */
	public function getCreateInfo(){
		$managerLoginInfo = model('Managers')->getLoginUserInfo();
	    return view('managerset/managerinfo',['managerLoginInfo'=>$managerLoginInfo]);
	}
	 
	/**
	 * 查看管理员个人资料页面
	 */ 
	public function getPersonInfo(){
		$manager_array_info['id'] = input('get.uuid/d');
		$manager_array_info['account'] = input('get.account');
		$personinfo = model('Managers')->getAllManagerInfo($manager_array_info);
		return view('managerset/personinfo',['personinfo'=>$personinfo]);
	}
	
	/**
	 * 修个人资料备注表单
	 */
	public function postSavePersoninfo(){
		$manager_info_array['profiles'] = input('post.profiles');
		$manager_id = input('post.id/d');
		if(model('Managers')->updateInfo($manager_info_array,$manager_id)){
			return json($this->returnAjax(1,'修改成功'));
		}
		return json($this->returnAjax(0,'修改失败'));
	}
	
	/**
	 * 修改密码页面
	 */
	public function getResetpwd(){
		return view('managerset/resetpwd');
	}
	
	/**
	 * 修改密码表单
	 */
	public function postSaveResetpwd(){
		//1.判断当前密码是否正确
		$param['id'] = model('Managers')->getLoginUserInfo('id');
		$param['password'] = input('post.currentPwd','','md5');
        $data = model('Managers')->getAllManagerInfo($param); 
		if($data===null) {
			return json($this->returnAjax(0,'当前密码输入错误'));
		}
		
		if(false == Validate::is(input('post.newPwd'), 'require')){
			return json($this->returnAjax(0 , '新密码不能为空'));
		}
		//2.判断两次输入的密码是否一致	
		if(input('post.newPwd') != input('post.confirmPwd')){
			return json($this->returnAjax(0 , '两次输入的密码不一致'));
		}
		
		//3.存入数据库
		$post_personal_pwd['password'] = input('post.newPwd','','md5');
		$re = model('Managers')->updateInfo($post_personal_pwd,$param['id']);
		if($re){
			return json($this->returnAjax(1,'修改密码成功'));
		}
			return json($this->returnAjax(0,'修改密码失败'));
	} 
	 
	 /**
	  * 设置用户状态
	  * @param 1-->解锁状态  0-->锁定状态
	  */
	public function setManagerStatus(){
		$manager_info_array['status'] = input('get.num/d');
		$manager_id = input('post.ids/a');
		if(model('Managers')->updateInfo($manager_info_array,$manager_id)){
			return json($this->returnAjax(1,'修改状态成功'));
		}
	} 
	 
	/**
	 * ajax返回的数据
	 */
	public function returnAjax($num,$msg){
		return ['status'=>$num,'message'=>$msg];		
	}
}
 

 ?>
